import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import javax.swing.JOptionPane;

/**
 * 
 */

/**
 * @author uniqtechnologies
 *
 */
public class RouterSendThread implements Runnable {

	private Socket receiver;
	private String message;
	Router r;

	RouterSendThread(Socket receiver, Router r,String message) {
		this.r = r;
		this.receiver = receiver;
		this.message=message;
	}
	@Override
	public void run() {
		
		
			//String message=r.Ing_data.getText();
			DataOutputStream dos;
			try {
			long startTime = System.nanoTime();
			dos = new DataOutputStream(receiver.getOutputStream());
			
			dos.writeUTF(message);
	
			DataInputStream di=new DataInputStream(receiver.getInputStream());
			String recv=di.readUTF();
			long endTime = System.nanoTime()-startTime;
			r.updateTime(endTime);
			JOptionPane.showInternalMessageDialog(r.cp1,recv);
			String q="update message_delivery set status='sent' where port="+r.port_number;
			Queries.update(q);
			if(recv.equalsIgnoreCase("message is already received by the receiver"))
			{
				r.Ing_data.setText(" ");
				JOptionPane.showInternalMessageDialog(r.cp1,"message deleted from the router");
			}
			}
			 catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		
		}
		
	

}
